# -*- coding: utf-8 -*-
import requests
import json
from bs4 import BeautifulSoup
from app.db.database import SessionLocal
from app.db.crud import create_tpost, get_posts_count_with_title
from app.pacong.content_fetcher import ContentFetcher
from typing import List, Optional

class ItHomeHotListFetcher(ContentFetcher):
    def __init__(self):
        self.content_type = 1
        self.url = "https://www.ithome.com/list/"


    def fetch_content(self):
        response = self._fetch_data(self.url)
        news_items = self._parse_data(response)
        return news_items

    def _fetch_data(self, url: str) -> str:
        response = requests.get(url)
        response.raise_for_status()
        return response.text

    def _parse_data(self, html: str):
        soup = BeautifulSoup(html, "html.parser")
        news_items = []

        for li in soup.select("#list > div.fl > ul > li"):
            a_tag = li.find("a", class_="t")
            url = a_tag.get("href") if a_tag else None
            title = a_tag.text.strip() if a_tag else None
            date_tag = li.find("i")
            date = date_tag.text.strip() if date_tag else None

            if url and title and date:
                is_ad = self._is_ad(url, title)
                if not is_ad:
                    pub_date = date
                    news_items.append({
                        "url": url,
                        "title": title,
                        "pub_date": pub_date
                    })
        return news_items

    def _is_ad(self, url: str, title: str) -> bool:
        ad_keywords = ["lapin", "神券", "优惠", "补贴", "京东"]
        return "lapin" in url or any(keyword in title for keyword in ad_keywords)





if __name__ == "__main__":
    fetcher = ItHomeHotListFetcher()
    fetcher.fetch_and_save()

    # 打印保存的内容
    # content = fetcher.fetch_content()
    # for item in content:
    #     print(json.dumps(item, ensure_ascii=False))